package com.taikang.hmp;
import java.security.Key; 

import javax.crypto.Cipher; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 

import sun.misc.BASE64Decoder; 
import sun.misc.BASE64Encoder; 

/**
 * 3DES解密工具类
 */
public class ThreeDes 
{ 
	
	
    private static BASE64Encoder base64 = new BASE64Encoder();
    private static byte[] myIV = {1,1,1,1,1,1,1,1};
    private static String strkey = "qwMDDbHe1P338s3d6zyrtgHU";
    private static String Algorithm = "DESede/CBC/PKCS5Padding";
    
    public static String desEncrypt(String input,String Strkey) throws Exception{ 
        
        DESedeKeySpec p8ksp = null;
        p8ksp = new DESedeKeySpec(Strkey.getBytes());
        Key key = null;
        key = SecretKeyFactory.getInstance("DESede").generateSecret(p8ksp);

        byte[] plainBytes = (byte[])null;
        Cipher cipher = null;
        byte[] cipherText = (byte[])null;
        
        plainBytes = input.getBytes("UTF-8");
        cipher = Cipher.getInstance(Algorithm);
        SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
        IvParameterSpec ivspec = new IvParameterSpec(myIV);
        cipher.init(1, myKey, ivspec);
        cipherText = cipher.doFinal(plainBytes);
        return removeBR(base64.encode(cipherText));       
    } 
     
    public static String desDecrypt(String cipherText ,String Strkey) throws Exception 
    { 
        
        BASE64Decoder base64d = new BASE64Decoder();
        DESedeKeySpec p8ksp = null;
        p8ksp = new DESedeKeySpec(Strkey.getBytes());
        Key key = null;
        key = SecretKeyFactory.getInstance("DESede").generateSecret(p8ksp);
        
        Cipher cipher = null;
        byte[] inPut = base64d.decodeBuffer(cipherText);
        cipher = Cipher.getInstance(Algorithm);
        SecretKeySpec myKey = new SecretKeySpec(key.getEncoded(), "DESede");
        IvParameterSpec ivspec = new IvParameterSpec(myIV);
        cipher.init(2, myKey, ivspec);
        byte[] output = removePadding(cipher.doFinal(inPut));

        return new String(output, "UTF-8");
        
    } 
     
    private static String removeBR(String str) {
        StringBuffer sf = new StringBuffer(str);

        for (int i = 0; i < sf.length(); ++i)
        {
          if (sf.charAt(i) == CommonConst.SEPARATOR_CHAR_RETURN)
          {
            sf = sf.deleteCharAt(i);
          }
        }
        for (int i = 0; i < sf.length(); ++i)
          if (sf.charAt(i) == CommonConst.SEPARATOR_CHAR_ENTER)
          {
            sf = sf.deleteCharAt(i);
          }

        return sf.toString();
      }

      public static byte[] removePadding(byte[] oldByteArray)
      {
        int numberPaded = 0;
        for (int i = oldByteArray.length; i >= 0; --i)
        {
          if (oldByteArray[(i - 1)] != 0)
          {
            numberPaded = oldByteArray.length - i;
            break;
          }
        }

        byte[] newByteArray = new byte[oldByteArray.length - numberPaded];
        System.arraycopy(oldByteArray, 0, newByteArray, 0, newByteArray.length);

        return newByteArray;
      }
      
    public static void main(String args[])
    { 
        try 
        { 
        	
       		 BASE64Encoder base64 = new BASE64Encoder(); 
       		 BASE64Decoder dbase64 = new BASE64Decoder();
       			
       		 //加密
            String desstr = ThreeDes.desEncrypt("̩泰康人寿保险股份有限公司",strkey);
            //base64
            String desstr64 = base64.encode(desstr.getBytes("UTF-8"));
            //base64解码
            String pstr = new String(dbase64.decodeBuffer(desstr64));
            //解密
            pstr = ThreeDes.desDecrypt(pstr,strkey);
            System.out.println("Encode:"+desstr);
            System.out.println("Encode:"+desstr64);
            System.out.println("Decode:"+pstr); 
        } 
        catch (Exception e) 
        { 
            e.printStackTrace(); 
        } 
    } 
} 

